package com.erp.controller;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.erp.entity.Demand;
import com.erp.entity.Label;
import com.erp.entity.Project;
import com.erp.enums.statusCode;
import com.erp.service.DemandService;
import com.erp.service.LabelService;
import com.erp.service.ProjectService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 查询统计 前端控制器
 * </p>
 *
 * @author CJ
 * @since 2021-08-19
 */
@Controller
@RequestMapping("/filter")
public class FilterCountController {
    @Resource
    ProjectService projectService;
    @Resource
    LabelService labelService;
    @Resource
    DemandService demandService;

    @GetMapping("list")
    @PreAuthorize("hasAnyAuthority('fileter:list')")
    public ModelAndView filterListView() {
        ModelAndView modelAndView = new ModelAndView();
        List<Project> projectList = projectService.list(new QueryWrapper<Project>()
                .eq("status",statusCode.Default.getValue()).select("id", "project_name"));
        List<Label> labelList = labelService.list(new QueryWrapper<Label>().eq("status",statusCode.Default.getValue())
                .select("label_name"));
        modelAndView.addObject("labelList", labelList);
        modelAndView.addObject("listProject", projectList);
        modelAndView.setViewName("system/filterCount/listView");
        return modelAndView;
    }
    // 列表数据
    @RequestMapping(value = "/getListData", method = RequestMethod.GET)
    @ResponseBody
    @PreAuthorize("hasAnyAuthority('fileter:list')")
    public Map<String, Object> getListData(
            @RequestParam(value = "page") int page,
            @RequestParam(value = "limit") int limit,
            @RequestParam(value = "labelName", required = false) String labelName,
            @RequestParam(value = "startTime", required = false) String startTime,
            @RequestParam(value = "endTime", required = false) String endTime,
            @RequestParam(value = "project", required = false) String project,
            @RequestParam(value = "status", required = false) String status) {
        IPage<Demand> demandPage = new Page<>(page, limit);//参数一是当前页，参数二是每页个数
        QueryWrapper<Demand> wrapper = new QueryWrapper<>();
        //标记一下是否有搜索条件
        boolean filter_status = false;
        if (StrUtil.isNotEmpty(startTime) && StrUtil.isNotEmpty(endTime)) {
            // 时间偏于一天
            Date date = DateUtil.parse(endTime,"yyyy-MM-dd");
            Date newDate = DateUtil.offset(date, DateField.DAY_OF_MONTH, 1);
            String formatDate = DateUtil.formatDate(newDate);
            wrapper.ge("create_time",startTime).le("create_time",formatDate);
            filter_status = true;
        }
        if (StrUtil.isNotEmpty(labelName)){
            wrapper.like("label_name",labelName);
            filter_status = true;
        }
        if (StrUtil.isNotEmpty(project)){
            wrapper.eq("projectName",project);
            filter_status = true;
        }
        if (StrUtil.isNotEmpty(status)){
            wrapper.eq("demand_status",Convert.toInt(status));
            filter_status = true;
        }
        wrapper.eq("del_status", statusCode.Default.getValue()).orderByDesc("create_time");
        demandPage = demandService.page(demandPage, wrapper);
        List<Demand> demandList = demandPage.getRecords();//获取数据，这里面还有当前页码数还有总数据参数
        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("msg", "");
        map.put("count", demandPage.getTotal());
        map.put("data", demandList);
        return map;
    }
}
